package SEApp {
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	
	public class se_sapp extends se_app {
		public static const REGSTARTCOMPONENT: String 	= 'REGSTARTCOMPONENT'; 
		public static const STARTCOMPLETE: String 		= 'STARTCOMPLETE';
		public static const STARTSTATE: String			= 'STARTSTATE';
		private var _startComponents: Array;
		 
		public function se_sapp(appWidth:uint, appHeight:uint, layerCount:uint=6) {
			super(appWidth, appHeight, layerCount);
		}
		
		override protected function initialize():void {
			_startComponents = new Array();
			addEventListener(REGSTARTCOMPONENT, doRegisterStartComponent);
			addEventListener(STARTCOMPLETE, doStartComplete);
			super.initialize();
		}
		
		protected function doRegisterStartComponent(e: ObjectEvent): void {
			registerStartComponent(e.object);
		}
		
		protected function doStartComplete(e: ObjectEvent): void {
			startComplete(e.object);
		}
		
		public function registerStartComponent(component: Object): void {
			if (_startComponents) _startComponents.push(component);
		}
		
		protected function startCompleteAll(): void {
			dispatchEvent(new Event(STARTSTATE));
			_startComponents = null;
		}
		
		public function startComplete(component: Object): void {
			if (_startComponents) {
				var i: int = 0;
				while (i < _startComponents.length) {
					if (_startComponents[i] == component) {
						_startComponents.splice(i, 1);
						if (isStartComplete) startCompleteAll();
						return;
					} else i++;
				}
			}
		}
		
		public function get isStartComplete(): Boolean {
			return !_startComponents || (_startComponents.length == 0);
		}
	}
}